<?php
use Yaf\Request\Simple;
define("__ROOT__",dirname(__FILE__));
try {
    require "vendor/autoload.php";
    if (php_sapi_name()!='cli') exit("请在cli模式下运行");
    $app = new Yaf\Application(__ROOT__ . "/conf/app.ini");
    define("APP_PATH",$app->getAppDirectory());
    define("PUBLIC_PATH",__ROOT__."/public/");
    define("RUNTIME_PATH",__ROOT__."/public/runtime/");
    define("CONFIG_PATH",__ROOT__."/conf/");
    $app->bootstrap();
    if (!isset($argv[1])) exit("Please enter the route to execute. Example: the php cli.php Index/Index!");
    $routeArr = explode('/', $argv[1]);
    if (count($routeArr) != 2) exit("Please enter the route to execute. Example: the php cli.php Index/Index!");
    $controllerName = $routeArr[0];
    $actionName     = $routeArr[1];
    // 删除路由参数。
    unset($argv[0], $argv[1]);
    $params = [];
    if (isset($argv[2])) parse_str($argv[2], $params);
    $request = new Simple('CLI', 'Cli', $controllerName, $actionName, $params);
    Yaf\Application::app()->getDispatcher()->returnResponse(true)->dispatch($request);
} catch (\Yaf\Exception $e) {
    exit($e->getMessage());
}
